package com.hzit.loan.task.job;

import com.hzit.payweb.api.resp.RespVo;
import com.hzit.payweb.api.resp.data.PayQueryRespData;
import com.hzit.payweb.api.resp.data.TradingData;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * 支付宝交易单笔查询：每隔5分钟去支付宝查询一下，支付中状态数据
 */
@Component

public class AliapySingelQueryJob {

    @Autowired
    private RestTemplate restTemplate;

    @XxlJob("aiapySingelQueryJob")
    public ReturnT<String> excute(String param){

        System.out.println("------------开始执行---------------");
        //1.获取所有支付状态为 支付中的数据. springCloud 调用
        String url = "http://127.0.0.1:1213/pay/queryTraing";

        String mchId = "1000";
        RespVo respVo =  restTemplate.getForObject(url, RespVo.class,mchId);
        if(respVo.getCode()!=0){
            System.out.println("失败："+respVo.getMsg() );
            return ReturnT.FAIL;
        }

      List<TradingData> dataList = (List<TradingData>) respVo.getData();

        if(CollectionUtils.isEmpty(dataList)){

            System.out.println("没有数据");

            return ReturnT.SUCCESS;
        }

        //开始处理

        String frontUrl = "http://127.0.0.1:1212/alipay/singleQuery";

        for (int i = 0;i<dataList.size();i++){
            //2.调用支付宝查询接口
            RespVo respVo1 =  restTemplate.getForObject(frontUrl,RespVo.class,dataList.get(i).getOutTradeNo(),dataList.get(i).getTradeNo());
            //更新数据
            if (respVo1.getCode()!=0){
                System.out.println("------");
                break;
            }

            //更新数据
            PayQueryRespData queryRespData = (PayQueryRespData)respVo1.getData();

            //调用pay-web的 更新接口

        }

        //3.更新流水状态

        return ReturnT.SUCCESS;

    }


}



